//
//  JCCollectionViewGridLayout.m
//  JCUIKit
//
//  Created by ChenJianjun on 15/11/26.
//  Copyright © 2015年 Boych<https://github.com/Boych>. All rights reserved.
//

#import "JCCollectionViewGridLayout.h"

@implementation JCCollectionViewGridLayout

- (instancetype)init
{
    if (self = [super init]) {
        self.minimumLeftSpacing = 8.f;
        self.minimumRightSpacing = 8.f;
    }
    return self;
}

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect
{
    NSArray *attributes = [self deepCopyWithArray:[super layoutAttributesForElementsInRect:rect]];
    CGFloat width = [self collectionViewContentSize].width;
    
    if (attributes.count > 0) { //第一个元素处理
        UICollectionViewLayoutAttributes *firstLayoutAttributes = attributes[0];
        firstLayoutAttributes.frame = [self resetAttributesFrame:firstLayoutAttributes.frame
                                                contentSizeWidth:width
                                                        prevMaxY:0];
    }
    for (NSInteger index = 1; index < attributes.count; index++) {
        UICollectionViewLayoutAttributes *prevLayoutAttributes = attributes[index - 1];
        UICollectionViewLayoutAttributes *currentLayoutAttributes = attributes[index];
        
        CGFloat currentOriginX = CGRectGetMaxX(prevLayoutAttributes.frame) + self.minimumInteritemSpacing;
        if (currentOriginX + CGRectGetWidth(currentLayoutAttributes.frame) + self.minimumRightSpacing <= width) {
            CGRect frame = currentLayoutAttributes.frame;
            frame.origin.x = currentOriginX;
            frame.origin.y = CGRectGetMinY(prevLayoutAttributes.frame);
            currentLayoutAttributes.frame = frame;
        } else { //上一行显示不下时换行
            currentLayoutAttributes.frame = [self resetAttributesFrame:currentLayoutAttributes.frame
                                                      contentSizeWidth:width
                                                              prevMaxY:CGRectGetMaxY(prevLayoutAttributes.frame)];
        }
    }
    return attributes;
}

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
    return !CGRectEqualToRect(newBounds, self.collectionView.bounds);
}

#pragma mark - Private

- (CGRect)resetAttributesFrame:(CGRect)frame
              contentSizeWidth:(CGFloat)contentSizeWidth
                      prevMaxY:(CGFloat)prevMaxY
{
    frame.origin.x = self.minimumLeftSpacing; //左对齐
    if (prevMaxY == 0) {
        frame.origin.y = prevMaxY;
    } else {
        frame.origin.y = prevMaxY + self.minimumLineSpacing;
    }
    CGFloat edgeWidth = self.minimumLeftSpacing + self.minimumRightSpacing;
    if (CGRectGetWidth(frame) > contentSizeWidth - edgeWidth) { //限制宽度较大的元素显示区域
        frame.size.width = contentSizeWidth - edgeWidth;
    }
    return frame;
}

- (NSArray *)deepCopyWithArray:(NSArray *)array
{
    if (array.count < 1) {
        return array;
    }
    NSMutableArray *copys = [NSMutableArray arrayWithCapacity:array.count];
    for (UICollectionViewLayoutAttributes *attris in array) {
        [copys addObject:[attris copy]];
    }
    return copys;
}

@end
